package com.ruoyi.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.Sequence;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface SequenceMapper extends BaseMapper<Sequence> {

    @Update("UPDATE sequence " +
            "SET value = value + 1, version = version + 1 " +
            "WHERE name = #{name} AND version = #{expectedVersion}")
    int optimisticIncrement(@Param("name") String name, @Param("expectedVersion") Integer version);

    @Select("SELECT * FROM sequence WHERE name = #{name}")
    Sequence getSequence(@Param("name") String name);

    @Select("SELECT * FROM sequence WHERE name = #{name} FOR UPDATE")
    Sequence selectForUpdate(@Param("name") String name);

}
